import com.wcarmon.life.event.*;
import com.wcarmon.life.model.*;
import com.wcarmon.utils.*;
import mx.controls.*;
import mx.collections.*;
import mx.utils.*;
import mx.events.*;

include "../../../../includes/loggerSnippet.as";



[Bindable]
public var searchFieldWidth : uint = 100;

[Bindable]
private var model : LifeModelLocator = LifeModelLocator.getInstance();
			
[Bindable]
public var filteredProjects : Array;

private var initialValue : int = -1;
private var passedValue : int = -1;






//////////////////////////////////////////////////////////////////////
private function doInit() : void {
	( new UpdateProjectComboBoxEvent() ).dispatch();	//GIVON: move this to command class???? which one?
	clearValue();	//GIVON: move to command class	
}

public function setInitialValue( v:int ) : void{
	log.debug('setting initial value for parent project field to ' + v);
	this.initialValue = v;
}



public function setValue( parentBlockId : String ) : void{
	log.debug('Setting ParentProject Field to ' + parentBlockId);
		
	if( Utils.empty(parentBlockId) ){
		log.debug('parentId is empty, setting parent drop-down to empty');
		projComboBox.selectedItem = 0;
		return;
	}
	
	var dataArray : Array = projComboBox.dataProvider.list.source;
		
	var success : Boolean = false;
	for( var i:uint=0; i < dataArray.length; i++ ){
		if( dataArray[i].data == parentBlockId ){	
			log.debug('found a match, setting parent project field!');
			projComboBox.selectedIndex = i;
			success = true;
			passedValue = uint( parentBlockId );
			continue;
		}
	}
	if( !success ){
		log.fatal('Code error: Cannot set parent project id#' + parentBlockId );
		log.info('possibly because of multiple parents???');
	}
}



public function getValue() : String {
	
	if( null == projComboBox.selectedItem ){
		log.debug('getvalue():nothing is selected for parent project field');
		return '';
	}
	var ans:String = projComboBox.selectedItem.data;
	if( ans is uint ){	//clean negative values
		return ans+'';
	}else{
		log.warn('Code/data error: selected index of ParentProjectField is invalid: ' + ans);
		return '';
	}
}



public function clearValue() : void {
	searchForProject.text = LifeModelLocator.INITIAL_SEARCH_BOX_STR;	//reset
	projComboBox.selectedIndex = 0;	 
}

public function isValid(): Boolean {
	return true;
}



private function filterProjects() : void {
	log.debug( 'Filtering parent project field for matches to ' + searchForProject.text );
	var s : String = searchForProject.text.toLowerCase(); //don't trim
	var o : Object;
	var i : int;
	filteredProjects = new Array();
	for (i = 0; i < model.projectComboBoxArray.length; i++) {
		o = model.projectComboBoxArray[i];
		if( o.content.toLowerCase().indexOf(s) != -1 ){
			filteredProjects.push(o);
			continue;
		}
		if( o.ref_meta.toLowerCase().indexOf(s) != -1 ){
			filteredProjects.push(o);
			continue;
		}
	}
	log.debug( "found " + filteredProjects.length + '-matching projets');
}


private function getProjectList( completeProjList : Array, filterText : String ) : ArrayCollection {
	var s3 : String = StringUtil.trim( searchForProject.text );
	var filterIsEmpty:Boolean = (s3 == ''); 
	var filterHasInitialValue:Boolean = (s3 == LifeModelLocator.INITIAL_SEARCH_BOX_STR);
	   
	if( filterIsEmpty || filterHasInitialValue ){
		log.debug('showing complete project list');
		if( initialValue is uint ){
			log.debug('hot-wiring parent project value ...again');
			callLater( setValue, [initialValue] );	//wait 1-frame and hot-wire
		}
		return new ArrayCollection( completeProjList );
	}else{
		filterProjects();
		return new ArrayCollection( filteredProjects );
  	}
}	


private function clearFilterField() : void {
	log.debug('clearing parent project search filter field');
	var filterHasInitialValue : Boolean = (searchForProject.text == LifeModelLocator.INITIAL_SEARCH_BOX_STR); 
	if( filterHasInitialValue ){
		searchForProject.text = '';
	}
} 
